package com.drg.center.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.drg.center.entity.param.SysDictItemListDTO;
import com.guaike.core.entity.enums.EnableStatusEnum;
import com.guaike.core.mybatis.plugins.BaseDAO;
import com.drg.center.entity.SysDictItemPO;
import com.drg.center.mapper.SysDictItemMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author: zhangxilin
 * @Date: 2023/10/25
 */
@Repository
public class SysDictItemDAO extends BaseDAO<SysDictItemMapper, SysDictItemPO> {

    public List<SysDictItemPO> findList(String dictCode){
        LambdaQueryWrapper<SysDictItemPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysDictItemPO::getDictCode,dictCode);
        lambdaQueryWrapper.eq(SysDictItemPO::getStatus, EnableStatusEnum.ENABLE.getCode());
        lambdaQueryWrapper.orderByAsc(SysDictItemPO::getSortIndex);
        return getBaseMapper().selectList(lambdaQueryWrapper);
    }
    public List<SysDictItemPO> findList(){
        LambdaQueryWrapper<SysDictItemPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysDictItemPO::getStatus, EnableStatusEnum.ENABLE.getCode());
        lambdaQueryWrapper.orderByAsc(SysDictItemPO::getSortIndex);
        return getBaseMapper().selectList(lambdaQueryWrapper);
    }

    public List<SysDictItemPO> list(SysDictItemListDTO param){
        LambdaQueryWrapper<SysDictItemPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysDictItemPO::getDictCode,param.getDictCode());
        if(StringUtils.isNotBlank(param.getItemName())){
            lambdaQueryWrapper.like(SysDictItemPO::getItemName, param.getItemName());
        }
        lambdaQueryWrapper.orderByAsc(SysDictItemPO::getSortIndex);
        return getBaseMapper().selectList(lambdaQueryWrapper);
    }

    public boolean isExist(String dictCode, String itemCode,Long excludeId){
        LambdaQueryWrapper<SysDictItemPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysDictItemPO::getDictCode,dictCode);
        lambdaQueryWrapper.eq(SysDictItemPO::getItemCode,itemCode);
        if(excludeId !=null){
            lambdaQueryWrapper.ne(SysDictItemPO::getId, excludeId);
        }
        return getBaseMapper().selectCount(lambdaQueryWrapper)>0? true: false;
    }

}
